package com.xjscrm.console.web.circle;


import com.xjscrm.common.exception.BusinessException;
import com.xjscrm.console.common.util.LoginContext;
import com.xjscrm.console.dto.circle.CircleTaskDTO;
import com.xjscrm.console.dto.circle.CircleTaskSearchDTO;
import com.xjscrm.console.service.customer.circle.CircleTaskService;
import com.xjscrm.console.vo.CircleTaskVO;
import com.xjscrm.console.vo.LoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * 朋友圈任务(CircleTask)表控制层
 *
 * @author szl
 * @since 2024-04-09 10:27:53
 */
@RestController
@RequestMapping("/circleTask")
@Api(tags = "朋友圈任务控制器")
public class CircleTaskController {
    /**
     * 服务对象
     */
    @Autowired
    private CircleTaskService circleTaskService;

    /**
     * 分页查询所有数据
     *
     * @param source 查询实体
     * @return 所有数据
     */
    @PostMapping("/search")
    @ApiOperation(value = "朋友圈任务分页查询", response = CircleTaskVO.class)
    public Object search(@RequestBody CircleTaskSearchDTO source) {
        if (StringUtils.isBlank(source.getOrderByField())) {
            source.setOrderByField("create_time");
            source.setAsc(false);
        }
        LoginVO login = LoginContext.getLogin();
        return circleTaskService.page(login.getMerId(), source);
    }

    /**
     * 通过主键查询单条数据
     *
     * @return 单条数据
     */
    @PostMapping("/get")
    @ApiOperation(value = "朋友圈任务详情", response = CircleTaskVO.class)
    public Object get(@RequestBody CircleTaskDTO source) {
        LoginVO login = LoginContext.getLogin();
        return this.circleTaskService.get(login.getMerId(), source.getId());
    }

    /**
     * 新增数据
     *
     * @param source 实体对象
     * @return 新增结果
     */
    @PostMapping("/add")
    @ApiOperation("新增朋友圈任务")
    public Object save(@RequestBody CircleTaskDTO source) {
        if (Objects.isNull(source) || ObjectUtils.isEmpty(source.getDeviceUniqueIds()) || Objects.isNull(source.getGroupId())
                || StringUtils.isBlank(source.getName())) {
            throw new BusinessException("创建朋友圈任务参数错误");
        }
        LoginVO login = LoginContext.getLogin();
        if (source.getDeviceUniqueIds().size() > 10) {
            throw new BusinessException("机器人最多只能选择10个");
        }
        return this.circleTaskService.add(login.getMerId(), login.getId(), source);
    }

    /**
     * 修改数据
     *
     * @param source 实体对象
     * @return 修改结果
     */
    @PostMapping("/update")
    @ApiOperation("修改朋友圈任务")
    public Object update(@RequestBody CircleTaskDTO source) {
        if (Objects.isNull(source) || Objects.isNull(source.getId()) || ObjectUtils.isEmpty(source.getDeviceUniqueIds()) || Objects.isNull(source.getGroupId())
                || StringUtils.isBlank(source.getName())) {
            throw new BusinessException("修改朋友圈任务参数错误");
        }
        if (source.getDeviceUniqueIds().size() > 10) {
            throw new BusinessException("机器人最多只能选择10个");
        }
        LoginVO login = LoginContext.getLogin();
        return this.circleTaskService.edit(login.getMerId(), login.getId(), source);
    }

    /**
     * 删除数据
     *
     * @return 删除结果
     */
    @PostMapping("/cancel")
    @ApiOperation("取消朋友圈任务")
    public Object cancel(@RequestBody CircleTaskDTO source) {
        LoginVO login = LoginContext.getLogin();

        return this.circleTaskService.cancel(login.getMerId(), source.getId());
    }

    @PostMapping("/remove")
    @ApiOperation(value = "删除朋友圈任务", response = Boolean.class)
    public Object remove(@RequestBody CircleTaskDTO source) {
        LoginVO login = LoginContext.getLogin();
        return this.circleTaskService.remove(login.getMerId(), source.getId());
    }
}

